

#include "mtalog.h"
#include <bautils.H>
#include <pathinfo.h>
 
_LIT(KLogFolder, "logs\\");
_LIT(KLogFileExt, ".log");
_LIT(KOldFileExt, ".old");
 
const TUint8 KBackSlash = '\\';
_LIT8(KLineEnd, "\r\n");
 
const TInt KTimeRecordSize = 20;
 
_LIT8(KTimeFormat,"%04d-%02d-%02d %02d:%02d:%02d ");
_LIT8(KLogStart, "--== New %S log ==--\r\n");
_LIT(KSquareBracket, "[");

CFileLogger * CFileLogger::iLogger = 0; 

CFileLogger::CFileLogger()
{
}
 
TBool CFileLogger::Construct()
{
	TInt err = iFs.Connect();
	if (!err)
	{
		TFileName logFileName;
		err = !GetLogFileNameWithoutExt(logFileName);
		if(!err)
		{
			TFileName oldLogFileName(logFileName);
			oldLogFileName.Append(KOldFileExt);
			iFs.Delete(oldLogFileName);
			logFileName.Append(KLogFileExt);
			iFs.Rename(logFileName, oldLogFileName);
			err = iFile.Create(iFs, logFileName, EFileShareAny | EFileWrite);
			if(!err)
			{
				TInt pos(0);
				iFile.Seek(ESeekEnd, pos);
				TBuf8<KMaxFullName> appName;
				appName.Copy(BaflUtils::ExtractAppNameFromFullName(RThread().FullName()));
				iLogBuffer.AppendFormat(KLogStart, &appName);
				err = iFile.Write(iLogBuffer);
			}
		}
	}
	return (!err);
}
 
CFileLogger* CFileLogger::Logger()
{
	if (iLogger == 0)
	{
		iLogger = new CFileLogger;
		if (iLogger)
		{
			if (iLogger->Construct())
			{
               //construction ok. do nothing
			}
			else
			{
				delete iLogger;
				iLogger = NULL;
			}
		}
	}
		return iLogger;
}
 
EXPORT_C void CFileLogger::Close()
{
	if(iLogger != 0)
    delete iLogger;
}
 
CFileLogger::~CFileLogger()
{
	iFile.Close();
	iFs.Close();
}
 
TBool CFileLogger::GetLogFileNameWithoutExt(TDes& aFileName)
{
 
	#ifndef _DEBUG
// Phone target
// You must create this folder for the logfile to be written:
// E:\Logs
	//aFileName.Copy(PathInfo::MemoryCardRootPath());
	aFileName.Copy(PathInfo::PhoneMemoryRootPath());
	#else

// Emulator target
// You must create this folder for the logfile to be written:
// C:\Symbian\9.1\S60_3rd_MR_2\Epoc32\winscw\c\Logs
	TChar drive;
	iFs.DriveToChar(EDriveC, drive);
	aFileName.Append(drive);
	const TUint8 KColon = ':';
	aFileName.Append(KColon);
	aFileName.Append(KBackSlash);
#endif
	aFileName.Append(KLogFolder);
	TBool res = BaflUtils::FolderExists(iFs, aFileName);
	if (!res)
	{
		aFileName.Zero();
 
	}
        else	
       {
		TPtrC fileName(BaflUtils::ExtractAppNameFromFullName(RThread().FullName()));
		aFileName.Append(fileName);
	}
	return res;
}
 
EXPORT_C void CFileLogger::Write(const TDesC8& aText)
{
	CFileLogger* logger = Logger();
	if(logger)
	{
		logger->iLogBuffer.Copy(aText);
		logger->DoWrite();
	}
}
 
EXPORT_C void CFileLogger::Write(const TDesC16& aText)
{
	CFileLogger* logger = Logger();
	if(logger)
	{
		logger->iLogBuffer.Copy(aText);
		logger->DoWrite();
	}
}
 
EXPORT_C void CFileLogger::Write(TRefByValue<const TDesC8> aFmt,...)
{
	CFileLogger* logger = Logger();
	if (logger)
	{
		VA_LIST list;
		VA_START(list, aFmt);
		logger->iLogBuffer.FormatList(aFmt, list);
		logger->DoWrite();
		VA_END(list);
	}
}
 
void CFileLogger::DoWrite()
{
	if(iFile.SubSessionHandle())
	{
		TTime time;
		time.HomeTime();
		TDateTime dateTime;
		dateTime = time.DateTime();
		TBuf8<KTimeRecordSize> timeRecord;
		timeRecord.Format(KTimeFormat, dateTime.Year(), dateTime.Month()+1, dateTime.Day()+1, dateTime.Hour(), dateTime.Minute(), dateTime.Second());
		iLogBuffer.Insert(0, timeRecord);
		iLogBuffer.Append(KLineEnd);
		iFile.Write(iLogBuffer);
		//do nothing if write fails...
#ifdef _DEBUG
		iFile.Flush();
#endif // _DEBUG
	}
}
